import { loginApi,getInfoApi } from "@/api/user/user"
import { LoginParam, LoginResult } from "@/api/user/userModel"
import { Result } from "@/http/request"
import { ActionContext } from "vuex"
import { RootState } from "../index"
import { setUserId, setToken, setExpireTime } from "@/utils/auth"

// mock获取
import { userInfo } from '@/api/mock/userInfo'

// 定义state类型
export type UserState = {
    token: string,
    userId: string,
    // permission: Array<string>
    permission: string[]
}

// 定义state
export const state: UserState = {
    token: '',
    userId: '',
    permission: []
}

// 定义mutations
export const mutations = {
    setToken(state:UserState,token:string) {
        state.token = token
    },
    setUserId(state:UserState,userId:string) {
        state.userId = userId
    },
    setRoles(state:UserState,roles:string[]) {
        state.permission = roles
    }
}

// 有接口的情况
// 定义actions. actions里面操作mutation，用ActionContext
export const actions111 = {
    // 获取用户信息
    getInfo({commit}: ActionContext<UserState,RootState>) {
        return new Promise((resolve,reject) => {
            getInfoApi().then(res => {
                // console.log(res)
                // 设置权限信息到vuex里面
                // 这里如果在api/user/userModel里的getInfoApi设置<UserInfo>，res.data就会自动提示roles
                if(res.code === 200) {
                    // console.log(res.data.roles)
                    commit('setRoles',res.data.roles)
                }
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    login({commit}: ActionContext<UserState,RootState>,loginParam:LoginParam) {
        return new Promise<Result>((resolve,reject) => {
            loginApi(loginParam).then(res => {
                // 把返回的token放到vuex里面
                // console.log(res.data)
                if(res.data.code === 200) {
                    console.log(res.data.token)
                    commit('setToken',res.data.token)
                    commit('setUserId',res.data.id)
                    // 存到cookies 或 sessionStorage
                    setToken(res.data.token)
                    setUserId(res.data.id)
                    setExpireTime(res.data.expireTime)
                }
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

// 无接口的情况
export const actions = {
    // 获取用户信息
    getInfo({commit}: ActionContext<UserState,RootState>) {
        return new Promise((resolve,reject) => {
            // getInfoApi().then(res => {
            //     // console.log(res)
            //     // 设置权限信息到vuex里面
            //     // 这里如果在api/user/userModel里的getInfoApi设置<UserInfo>，res.data就会自动提示roles
            //     if(res.code === 200) {
            //         commit('setRoles',res.data.roles)
            //     }
            //     resolve(res.data)
            // }).catch(err => {
            //     reject(err)
            // })
            // console.log(userInfo.data.roles)
            commit('setRoles',userInfo.data.roles)
            resolve(userInfo.data)
           
        })
    },
    login({commit}: ActionContext<UserState,RootState>,loginParam:LoginParam) {
        return new Promise<Result>((resolve,reject) => {
            // loginApi(loginParam).then(res => {
            //     // 把返回的token放到vuex里面
            //     // console.log('1111111')
            //     console.log(res.data)
            //     if(res.data.code === 200) {
            //         console.log(res.data.token)
            //         commit('setToken',res.data.token)
            //         commit('setUserId',res.data.id)
            //         // 存到cookies 或 sessionStorage
            //         setToken(res.data.token)
            //         setUserId(res.data.id)
            //         setExpireTime(res.data.expireTime)
            //     }
            //     resolve(res)
            // }).catch(err => {
            //     reject(err)
            // })
            const res = {
                data: {
                    code: 200,
                    expireTime: 1704149962000,
                    id: 9,
                    token: "testToken"
                }
            }
            commit('setToken','testToken')
            commit('setUserId',123)
            setToken('setToken')
            setUserId(123)
            setExpireTime(24*60*60)
            resolve(res.data as any)
        })
    }
}

// 定义getters
export const getters = {
    // 获取用户的权限字段
    getPermissions(state: UserState){
        return state.permission
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}


